package com.example.ex3;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener{
    Handler handler;
    ListView listView;
    HashMap<String,String> map;
    private static final String TAG = "RateList";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate2);

        //初始化数据项
        ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < 30; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", "Rate:" + i);
            map.put("ItemDetail", "Detail:" + i);
            listItems.add(map);
        }

        SimpleAdapter listItemAdapter = new SimpleAdapter(this,
                listItems,
                R.layout.list_item,
                new String[]{"ItemTitle","ItemDetail"},
                new int[]{R.id.title, R.id.detail}
        );

        listView = findViewById(R.id.mylist2);
        listView.setAdapter(listItemAdapter);
        listView.setOnItemClickListener(this);

        handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.what==7){
                    ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                    //构造Adapter
                    SimpleAdapter listItemAdapter = new SimpleAdapter(RateActivity2.this,
                            list,
                            R.layout.list_item,
                            new String[]{"cname","cval"},
                            new int[]{R.id.title,R.id.detail}
                    );
                    listView.setAdapter(listItemAdapter);
                }
                super.handleMessage(msg);
            }
        };
        MapTask mapTask = new MapTask();
        mapTask.setHandler(handler);

        Thread t = new Thread(mapTask);
        t.start();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG,"onitemclick");
        map = (HashMap<String, String>) listView.getItemAtPosition(position);
        Log.i(TAG,"onitemclick:cname="+map.get("cname"));
        Log.i(TAG,"onitemclick:cval="+map.get("cval"));

        Intent intent = new Intent(this,RateChangeActivity.class);

        intent.putExtra("cname",map.get("cname"));
        Log.i(TAG,"open:cname"+map.get("cname"));
        intent.putExtra("number",map.get("cval"));
        Log.i(TAG,"open:cval"+map.get("cval"));

        //    startActivity(intent);
        startActivityForResult(intent,3);
    }
}